/*
Copyright (C) 2010 
Author		  Oliver Schenk
Contact		  Oliver.Schenk76@gmail.com

This program is free software; you can redistribute
it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; 
if not, see <http://www.gnu.org/licenses/>.
*/


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.net.MalformedURLException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.util.*" %>
<%@ page import="com.google.gdata.client.calendar.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.client.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.model.*" %>
<%@ page import="com.google.gdata.util.*" %>
<%@ page import="com.google.gdata.data.calendar.*" %>
<%@ page import="com.google.gdata.data.extensions.*" %>
<%@ page import="com.google.gdata.data.*" %>
<%@ page import="com.google.gdata.client.http.*" %>
<html>
	<title>Selbstbucher</title>
  <head>
      <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>
  <body>

<%
		try{
			Boolean belegt = new Boolean(false);
			
			// Set up the URL and the object that will handle the connection:
			//URL feedUrl = new URL("http://www.google.com/calendar/feeds/oliver.schenk@web.de/private/TEST2");
			URL feedUrl = new URL("http://www.google.com/calendar/feeds/mktno9ucgijuj2tscgud1os22k@group.calendar.google.com/private/full");
			CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
			myService.setUserCredentials("oliver.schenk@web.de", "DK3172GL");
			
			// Send the request and receive the response:
			CalendarEventFeed myFeed = myService.getFeed(feedUrl, CalendarEventFeed.class);
			ArrayList Events = new ArrayList();
			for (int i = 0; i < myFeed.getEntries().size(); i++) {
				CalendarEventEntry entry = myFeed.getEntries().get(i);
	
	            List timeList = entry.getTimes(); 
				for (int j=0; j<timeList.size();j++){
					When eventTime = (When) timeList.get(j); 
					DateTime startTime = eventTime.getStartTime(); 
					DateTime endTime = eventTime.getEndTime(); 
					String startTimeStr = startTime.toUiString(); 
					String endTimeStr = endTime.toUiString(); 
					Events.add(startTimeStr);
				}				  
			}
			for(int i=0; i<Events.size(); i++){
			%>
				###
				<%=Events.get(i)%>
				###</br>
			<%
			}
			
			%>
			<p>Your calendarsgggggggggggggg:</p>
			</br><p>
			<%
			
			ArrayList OpeningHours = new ArrayList(); 
			OpeningHours.add("08:00"); 
			OpeningHours.add("09:00");
			OpeningHours.add("10:00");
			OpeningHours.add("11:00");
			OpeningHours.add("12:00");
			OpeningHours.add("13:00");
			OpeningHours.add("14:00");
			OpeningHours.add("15:00");
			OpeningHours.add("16:00");
			// get array 
			//Object OpeningHours[] = OpeningHours.toArray(); 
			
			// sum the array 
			for(int k=0; k<OpeningHours.size(); k++){
			%>
				
				<%
				for(int i=0;i<Events.size();i++){
					String tmp = Events.get(i).toString();
					String tmp1 = OpeningHours.get(k).toString();
					belegt = new Boolean(false);
					if(tmp.contains(tmp1)){
						belegt = new Boolean(true);
						break;
					}
				}
				if(belegt){
				%>  
					<%=OpeningHours.get(k)%>   Belegt</br>
				<%	
				}else{
				%>
					<%=OpeningHours.get(k)%>   Frei</br>
				<%
				}
			} 
		}
		catch(Exception e) {
			System.out.println(e.getMessage());
		}
%>

</p>
  </body>
</html>